﻿using System.Windows;
using System.Windows.Controls;
using GameEngine.Clients.Silverlight;
using GameEngine.Model.Data;
using GameEngine.Clients.Silverlight.View.Components;
using GameEngine.Clients.Silverlight.View;
using MySettlers.View.Components;
using GameEngine.View.Components;

namespace MySettlers
{
    public partial class NewThingUC : ChildWindow, IMenuView<string>
    {
        public const string OKBUTTON = "EXITBUTTON";
        public const string CANCELBUTTON = "NEWLEVELBUTTON";
        public event OnButtonClickHandler<string> OnExternalActionButtonClick;
        public NewThingUC()
        {
            InitializeComponent();
            OKButton.Tag = OKBUTTON;
            CancelButton.Tag = CANCELBUTTON;
            AddEntry(MyFacade.Instance.grassTypeId);
            AddEntry(MyFacade.Instance.treeTypeId);
            AddEntry(MyFacade.Instance.houseTypeId);
            AddEntry(MyFacade.Instance.fieldTypeId);
        }
        public int? SelectedThingTypeId=null;
        void AddEntry(ThingTypeVOId typeId)
        {
            Button b = new Button();
            b.Tag = typeId;
            b.Content = new Canvas();

            IThingVO<int> thing = new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()) { Name = typeId.Name }, typeId);
            //SilverlightThingMediator med = SilverlightViewFacade.Instance.CreateItemMediator(null) as SilverlightThingMediator;
            var view = SilverlightViewFacade.Instance.CreateViewInstance();
            IViewVO<int> viewVO = SilverlightModelFacade.Helper.RetrieveViewVO(
                SilverlightModelFacade.Helper.RetrieveViewFromThingType(thing.TypeId)
            );
            view.Initialize(viewVO);

            //med.
            //SilverlightApplicationFacadeHelper.AddThingToWorld(thing, MySmurfsFacade.Instance.MenuWorld);
            
            ((Canvas)b.Content).Children.Add(
                view as UIElement
                );
            //((Canvas)b.Content).RenderTransform = new TransformGroup();
            //((TransformGroup)((Canvas)b.Content).RenderTransform).Children.Add(new TranslateTransform() {
            //    Y = item.Height
            //});
            //b.Height = item.Height;
            b.Height = 60;
            b.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) {
                Button b2 = sender as Button;
                SelectedThingTypeId = (b2.Tag as ThingTypeVOId).Value;
            });
            thingContainer.Children.Add(b);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if(OnExternalActionButtonClick!=null)
                OnExternalActionButtonClick((string)b.Tag);
        }
    }
}

